package com.yexiao.core.util;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;

import java.math.BigDecimal;

public class FormatUtil {

    public static Integer toInt(Object value) {
        return toInt(value, (Integer)null);
    }

    public static Integer toInt(Object value, Integer defValue) {
        if (value instanceof Integer) {
            return (Integer)value;
        } else {
            Double doubleValue = Convert.toDouble(value);
            return doubleValue == null ? defValue : doubleValue.intValue();
        }
    }

    public static Long toLong(Object value) {
        return toLong(value, (Long)null);
    }

    public static Long toLong(Object value, Long defValue) {
        if (value == null) {
            return defValue;
        } else {
            try {
                return (new BigDecimal(value.toString())).longValue();
            } catch (Exception var3) {
                return defValue;
            }
        }
    }

    public static String toStr(Object value) {
        return toStr(value, (String)null);
    }

    public static String toStr(Object value, String defValue) {
        if (value instanceof String) {
            return (String)value;
        } else {
            String str = Convert.toStr(value);
            return StrUtil.isEmpty(defValue) ? defValue : str;
        }
    }
}
